<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

		  <p-col  :span="12">
			<p-form-item label="产品类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'productType', validatorRules.productType]" placeholder="请输入产品类型"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="产品类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'productCategory', validatorRules.productCategory]" placeholder="请输入产品类别"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['processType', validatorRules.processType]" :trigger-change="true" dictCode="mes_process_type" placeholder="请选择工单类型"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="任务来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'taskSource', validatorRules.taskSource]" placeholder="请输入任务来源"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="自制/外协" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'selfControl', validatorRules.selfControl]" placeholder="请输入自制/外协"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'productionno', validatorRules.productionno]" placeholder="请输入工单号"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工序工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'processbillno', validatorRules.processbillno]" placeholder="请输入工序工单号"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="二级工序编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'processTwocode', validatorRules.processTwocode]" placeholder="请输入二级工序编码"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="二级工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'processTwoname', validatorRules.processTwoname]" placeholder="请输入二级工序名称"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="线体编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="basic-business"
						v-decorator="['lineno', validatorRules.lineno]"
						:trigger-change="true"
						org-fields="lineid,lineno,linename"
						dest-fields="lineid,lineno,linename"
						code="line_select"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="线体名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'linename', validatorRules.linename]" placeholder="请输入线体名称"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="BOM版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'bomversions', validatorRules.bomversions]" placeholder="请输入BOM版本"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工单状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['billnoStatus', validatorRules.billnoStatus]" :trigger-change="true" dictCode="mes_billno_status" placeholder="请选择工单状态"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="计划开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择计划开始时间" v-decorator="[ 'starttime', validatorRules.starttime]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="计划完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择计划完成时间" v-decorator="[ 'completiontime', validatorRules.completiontime]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料齐套状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['qistate', validatorRules.qistate]" :trigger-change="true" dictCode="mes_qistate" placeholder="请选择物料齐套状态"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="计划数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'planQty', validatorRules.planQty]" placeholder="请输入计划数量"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="完成进度" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'schedule', validatorRules.schedule]" placeholder="请输入完成进度"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="BOM状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'bomstatus', validatorRules.bomstatus]" placeholder="请输入BOM状态" style="width: 100%"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
			</p-form-item>
		  </p-col>


        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="工序加工详情" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesMfProcessDetailsTable.loading"
            :columns="mesMfProcessDetailsTable.columns"
            :dataSource="mesMfProcessDetailsTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="排班人员信息(上岗人员)" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="mesMfCrewSchedulingTable.loading"
            :columns="mesMfCrewSchedulingTable.columns"
            :dataSource="mesMfCrewSchedulingTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesMfProcessModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          productType: {rules: [
          ]},
          productCategory: {rules: [
          ]},
          processType: {rules: [
          ]},
          taskSource: {rules: [
          ]},
          selfControl: {rules: [
          ]},
          productionno: {rules: [
          ]},
          processbillno: {rules: [
          ]},
          processTwocode: {rules: [
          ]},
          processTwoname: {rules: [
          ]},
          lineno: {rules: [
          ]},
          linename: {rules: [
          ]},
          bomversions: {rules: [
          ]},
          billnoStatus: {rules: [
          ]},
          starttime: {rules: [
          ]},
          completiontime: {rules: [
          ]},
          qistate: {rules: [
          ]},
          planQty: {rules: [
          ]},
          schedule: {rules: [
          ]},
          bomstatus: {rules: [
          ]},
          remark: {rules: [
          ]},
        },
        refKeys: ['mesMfProcessDetails', 'mesMfCrewScheduling', ],
        tableKeys:['mesMfProcessDetails', 'mesMfCrewScheduling', ],
        activeKey: 'mesMfProcessDetails',
        // 工序加工详情
        mesMfProcessDetailsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '工序工单',
              key: 'processOrder',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: 'SN/PN号',
              key: 'sn',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '计划数量',
              key: 'plannedQty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 排班人员信息(上岗人员)
        mesMfCrewSchedulingTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '排班人员',
              key: 'schedulingPersonnel',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '上岗人员',
              key: 'mountguardPersonnel',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '匹配结果',
              key: 'matchingResults',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '上岗时间',
              key: 'dutyTime',
              type: FormTypes.datetime,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '迁出时间',
              key: 'outTime',
              type: FormTypes.datetime,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: '/mes-manufacture/auto/mesMfProcess/add',
          edit: '/mes-manufacture/auto/mesMfProcess/edit',
          mesMfProcessDetails: {
            list: '/mes-manufacture/auto/mesMfProcess/queryMesMfProcessDetailsByMainId'
          },
          mesMfCrewScheduling: {
            list: '/mes-manufacture/auto/mesMfProcess/queryMesMfCrewSchedulingByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'productType','productCategory','processType','taskSource','selfControl','productionno','processbillno','processTwocode','processTwoname','lineno','linename','mcode','mname','bomversions','billnoStatus','starttime','completiontime','qistate','planQty','schedule','bomstatus','createBy','createTime','updateBy','updateTime','remark')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesMfProcessDetails.list, params, this.mesMfProcessDetailsTable)
          this.requestSubTableData(this.url.mesMfCrewScheduling.list, params, this.mesMfCrewSchedulingTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesMfProcessDetailsList: allValues.tablesValue[0].values,
          mesMfCrewSchedulingList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'productType','productCategory','processType','taskSource','selfControl','productionno','processbillno','processTwocode','processTwoname','lineno','linename','mcode','mname','bomversions','billnoStatus','starttime','completiontime','qistate','planQty','schedule','bomstatus','createBy','createTime','updateBy','updateTime','remark'))
     },

    }
  }
</script>

<style scoped>
</style>
